package view;

import controller.ClientController;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import model.MessageOutput;

/**
 *
 * @author Dustin & Maik
 */
public class ClientView extends JFrame implements MessageOutput {
    private JTextArea outputArea;
    private JTextField inputArea;

    /**
     * Constructs a new ClientView and sets everything up.
     */
    public ClientView(ClientController controller) {
        outputArea = new JTextArea();
        outputArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(outputArea);

        inputArea = new JTextField();
        inputArea.setActionCommand("INPUT");
        inputArea.addActionListener(controller);

        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(inputArea, BorderLayout.SOUTH);

        setSize(512,512);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        inputArea.requestFocus();
    }

    /**
     * Command to add the given String to a JTextArea.
     * @param message the String to be added
     */
    public void addMessage(String message) {
        outputArea.append(message += message.endsWith("\n") ? "" : "\n");
    }
}
